package com.example.customviewwithvariousshader;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RoundRectShape;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.MotionEvent;

/**
 * @author COCO
 * @date 2018/11/28
 */
public class PeekThroughImageView extends AppCompatImageView {
    private final float RADIUS = 20;
    private Paint paint = null;
    private float x;
    private float y;
    private float density;
    private int width;
    private int height;

    public PeekThroughImageView(Context context) {
        this(context, null);
    }

    public PeekThroughImageView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PeekThroughImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        density = context.getResources().getDisplayMetrics().density;

        paint = new Paint();
        paint.setAntiAlias(true);
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        int action = motionEvent.getAction();
        x = motionEvent.getX();
        y = motionEvent.getY();
        //invalidate();
        return performClick();
    }

    @Override
    public boolean performClick() {
        super.performClick();
        return true;
    }

    private Bitmap bitmap;
    private BitmapShader bitmapShader;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        this.width = w;
        this.height = h;

        bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.index_img_plus);
        bitmapShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
    }

    /**
     * 开发过程中需要对Bitmap进行缩放，合理占用内存
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.GRAY);

        //Demo1遮罩
        //Bitmap original = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        //Canvas originalCanvas = new Canvas(original);
        //originalCanvas.drawColor(Color.RED);
        //Shader shader = new BitmapShader(original, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        //paint.setShader(bitmapShader);
        //canvas.drawCircle(x, y - RADIUS * density, RADIUS * density, paint);


        //Demo2圆角
        //方式一
        //float[] radii = new float[]{50, 50, 50, 50, 50, 50, 50, 50};
        //RoundRectShape mRoundShape = new RoundRectShape(radii, null, null);
        //mRoundShape.resize(width, height);
        //mRoundShape.draw(canvas, paint);

        //方式二
        //RectF rect = new RectF(0, 0, width, height);
        //canvas.drawRoundRect(rect, 50, 50, paint);


        //Demo3圆形
        //方式一
        //canvas.drawCircle(width / 2, height / 2, width / 2, paint);

        //方式二
        //OvalShape ovalShape = new OvalShape();
        //ovalShape.resize(width, height);
        //按照渲染器和Shape形状，渲染画板
        //ovalShape.draw(canvas, paint);

        //方式三 xfermode实现圆形
        //加个描边
        paint.setColor(Color.RED);
        canvas.drawOval(0, 0, width, height, paint);

        //将绘制操作保存到新的图层（离屏缓存）
        int sc = canvas.saveLayer(10, 10, width - 10, height - 10, null, Canvas.ALL_SAVE_FLAG);

        //使用遮罩图层（画一个圆，再给一个Bitmap）
        canvas.drawOval(10, 10, width - 10, height - 10, paint); //DST
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 10, 10, paint);//SRC
        paint.setXfermode(null);

        // 还原画布
        canvas.restoreToCount(sc);

        //Demo3椭圆形
//        OvalShape ovalShape = new OvalShape();
//        ovalShape.resize(width / 2, height);
//        ShapeDrawable shapeDrawable = new ShapeDrawable(ovalShape);
//        //得到画笔并设置着色器
//        shapeDrawable.getPaint().setShader(bitmapShader);
//        //设置图像显示的区域
//        shapeDrawable.setBounds(width / 2, 0, width, height);
//        //将shapeDrawable的内容绘制到canvas上
//        shapeDrawable.draw(canvas);
    }
}
